﻿
#region

using System.Collections.Generic;
using System.Linq;
using UserStoryTrackerDemo.Domain.Core.Pocos;
using UserStoryTrackerDemo.Domain.Core.Repository;

#endregion

namespace UserStoryTrackerDemo.Data
{
	public class Repository<TPoco> : IRepository<TPoco> where TPoco : IPoco
	{
		private readonly List<TPoco> _db;

		public Repository(List<TPoco> db)
		{
			_db = db;
		}

		public List<TPoco> GetAll()
		{
			return _db;
		}

		public TPoco Save(TPoco tPoco)
		{
			int indx = Database.Instance.GetUserStoryData().Count;
			int id = Database.Instance.GetUserStoryData()[indx - 1].Id + 1;

			tPoco.Id = id;
			//_db.Add(tPoco);

			Database.Instance.SavePoco(tPoco);

			return tPoco;
		}

		public TPoco GetById(int id)
		{
			var result = (from tPoco in _db
			              where tPoco.Id == id
			              select tPoco).FirstOrDefault();

			return result;
		}

		public TPoco Update(TPoco tPoco)
		{
			Database.Instance.UpdatePoco(tPoco);
			return tPoco;
		}
	}
}